const { UI_Layer, UI_Listen } = require("../../config/ui_def");

cc.Class({
    extends: cc.Component,

    properties: {
        episode: -1,           // 集数
        scroll: cc.ScrollView,
        check_next: false,     // 监听
        check_time: 0,         // 计数
        count_time: 0,         // 监听时间
        only_read: false,      // 只是查看漫画

        click_mask_node: cc.Node,
        bar_sprite: cc.Sprite,
        tip_node: cc.Node,
        next_button: cc.Node,
        bar_node: cc.Node,
        tip_box_widget: cc.Widget
    },
    initPage() {
        if (!he.pool.getPool('manhuaNode')) {
            he.pool.createPoolSync('manhuaNode', 5, cc.Prefab, 'uimod/manhuaNode')
        }
    },
    on_scrolling(event) {
        if (event) {
            cc.log(event.getScrollOffset().toString())
        }
    },
    updatePage(param) {
        gg.data.isManHua = true
        this.loadlist = []
        this.page_index = 0
        this.episode = param.episode

        this.only_read = param.only_read
        this.page_list = he.db.getDataListByArgs("Manhua", { episode: param.episode })
        // console.log(this.page_list)
        this.page_list.sort((a, b) => {
            return a.order - b.order
        })
        let load_plist = []
        for (let i = 0; i < this.page_list.length; i++) {
            if (load_plist.indexOf(this.page_list[i].plist) < 0 && this.page_list[i].plist) {
                load_plist.push(this.page_list[i].plist)
            }
        }
        let load_voice = []
        for (let i = 0; i < this.page_list.length; i++) {
            if (load_voice.indexOf(this.page_list[i].music) < 0 && this.page_list[i].music) {
                load_voice.push(this.page_list[i].music)
            }
            if (load_voice.indexOf(this.page_list[i].effect) < 0 && this.page_list[i].effect) {
                load_voice.push(this.page_list[i].effect)
            }
        }
        for (let i = 0; i < load_plist.length; i++) {
            this.loadlist.push({
                url: 'plist/' + load_plist[i], type: cc.SpriteAtlas, bundle: 'comic'
            })
        }
        // console.log(this.loadlist)
        // for (let i = 0; i < load_voice.length; i++) {
        //     this.loadlist.push({
        //         url: 'voice/manhua/' + load_voice[i], type: cc.AudioClip,
        //     })
        // }
        this.tip_box_widget.updateAlignment()
        he.load.loadResList(this.loadlist, this.checkLoadover.bind(this))
    },
    checkLoadover() {
        if (he.load.checkLoadOver(this.loadlist)) {
            gg.eventManager.emit('loadcover', UI_Listen.CLOSE_LOAD_COVER)
            this.initSContent()
            this.nextShow()
        }
    },
    initSContent() {
        let setY = 0
        for (let i = 0; i < this.page_list.length; i++) {
            let info = this.page_list[i]
            // info.wait = 1
            let fixY = setY + info.fixY + common.MANHUA_MARGIN
            let manhuaPrefab = he.pool.getElement('manhuaNode')
            this.scroll.content.addChild(manhuaPrefab)
            manhuaPrefab.zIndex = info.zindex
            manhuaPrefab.y = -fixY
            let manhuaScript = manhuaPrefab.getComponent('manhuaNode')
            manhuaScript.initInfo(this.page_list[i])
            setY = manhuaPrefab.height + fixY
        }
        this.scroll.content.height = setY + common.MANHUA_MARGIN
    },
    overClick() {
        if (this.only_read) {
            he.voice.playMusic('bgm', 'voice')
            this.uiBase.closePage()
            return
        }
        switch (this.episode) {
            case 0:
                // gg.uiManager.addPage(UI_Layer.Load, {
                //     name: "LoadCover",
                //     in: true,
                //     scene: 'Main'
                // })
                this.uiBase.closePage()
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_NEWROLE })
                break
            default:
                this.uiBase.closePage()
                break
        }
    },
    nextShow() {
        if (!this.page_list[this.page_index]) {
            this.scroll.scrollToBottom(0.1)
            this.tip_node.active = false
            this.next_button.active = true
            this.click_mask_node.active = false
            return
        }
        let info = this.page_list[this.page_index]
        let manahuaNode = this.scroll.content.getChildByName(info.order + "")
        let manhuaScript = manahuaNode.getComponent('manhuaNode')
        manhuaScript.show()
        this.page_index += 1

        let scrollHeight = this.scroll.content.height
        // 屏幕中点高度
        let winHeightCenter = gg.uiManager.canvasSize.height / 2
        // 滚动最低点高度
        let FinY = scrollHeight - this.node.height
        // he.log(FinY)
        let toY = 0
        for (let i = 0; i < this.page_list.length; i++) {
            if (this.page_list[i].order > info.order) {
                break
            }
            let preNode = this.scroll.content.getChildByName(this.page_list[i].order + "")
            toY += preNode.height + this.page_list[i].fixY + common.MANHUA_MARGIN
        }
        toY -= winHeightCenter
        if (toY > FinY) {
            this.scroll.scrollToOffset(cc.v2(0, FinY), 0.5)
        } else if (toY < winHeightCenter) {
            this.scroll.scrollToOffset(cc.Vec2.ZERO, 0.5)
        } else {
            this.scroll.scrollToOffset(cc.v2(0, toY - winHeightCenter), 0.5)
        }
        if (info.wait) {
            this.check_next = true
            this.count_time = info.wait
            this.bar_node.active = true
            this.bar_sprite.fillRange = 0
        } else {
            this.bar_node.active = false
            this.check_next = false
            this.check_time = 0
            this.nextShow()
        }
    },
    click_mask() {
        this.check_time += 1
    },
    update(dt) {
        if (this.check_next) {
            this.check_time += dt
            this.bar_sprite.fillRange = this.check_time / this.count_time
            if (this.check_time > this.count_time) {
                this.count_time = 0
                this.check_time = 0
                this.check_next = false
                this.nextShow()
            }
        }
    },
    closeUI() {
        let guideList = he.local.getJson('guideList', 'user')
        if (!guideList) {
            return
        } else {
            if (!guideList[0]) {
                //新手引导
                guideList[0] = 1
                he.local.setJson('guideList', guideList, 'user')
            } 
        }
        this.only_read = false
    },
});
